package dxunderground.utilities.server;

import java.io.IOException;
import java.net.ServerSocket;

public class Server implements Runnable {
	private int port = 4444;
	private boolean listening = true;

	private Server() {
	}

	public void setListening(boolean listening) {
		this.listening = listening;
	}

	public void run() {
		ServerSocket serverSocket = null;

		try {
			serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			System.err.println("Could not listen on port: " + port);
			System.exit(-1);
		}

		System.out.println("Searching for clients.");

		while (listening) {
			try {
				new Thread(new ServerThread(serverSocket.accept())).start();
			} catch (IOException e) {
				System.out.println(e);
			}
		}
		try {
			serverSocket.close();
		} catch (IOException e) {
			System.out.println(e);
		}

		System.out.println("Server stopped.");
	}
}